;+
; NAME:
;       SET_KDA_NOTE_FLAG
;
; PURPOSE:
;       Sets the "kda_notes" flag in the IDL structure
;
; CATEGORY:
;       BGPS distance-omnibus Utility
;
; CALLING SEQUENCE:
;       new_flag = set_kda_note_flag( flag [,f1][,f2][,f3][,f4][,...] )
;
; INPUTS:
;       flag -- .kda_notes structure member for the BGPS source
;       f1   -- !KDAN flag to be set (see README_bgps_distance.txt)
;       f2   -- Next !KDAN flag to be set ... (and so on, and so forth)
;
; OPTIONAL INPUTS:
;       NONE
;
; KEYWORD PARAMETERS:
;       VERBOSE -- Be verbose with output
;
; OUTPUTS:
;       new_flag -- new flag for IDL structure
;
; OPTIONAL OUTPUTS:
;       NONE
;
; EXAMPLE:
;       s.kda_notes = set_kda_note_flag(s.kda_notes, 'tp','ir24')
;
; NOTES:
;       Routine is written such that new flag definitions in
;       README_bgps_distance.txt no longer require updates here.
;       * At present, up to 10 flags may be set at once.  Expand f*
;       list in calling sequence if more are needed in the future.
;
; MODIFICATION HISTORY:
;       Created:  06/04/10, TPEB -- Initial version
;       Modified: 06/22/10, TPEB -- Changed from PRO to FUNCTION so
;                                   that return value can be fed into
;                                   IDL strucutre (issue with pointers)
;       Modified: 07/08/10, TPEB -- Made routine independent of
;                                   changing (and added) flag
;                                   definitions in the README file.
;-

FUNCTION SET_KDA_NOTE_FLAG, flag, f1,f2,f3,f4,f5,f6,f7,f8,f9,f10, $
                            VERBOSE=verbose
  
  ;; Count how many new flags to add
  n_add = N_PARAMS()-1
  
  ;; Check if flag definitions already loaded
  defsysv, '!KDAN', EXISTS=exists
  IF ~ EXISTS THEN define_bitwise_flags
  
  ;; Load defined flags into array
  def_flags = TAG_NAMES(!KDAN)
  n_flags = N_ELEMENTS(def_flags)
  
  ;; Loop over new flags to be added
  FOR i=1L, n_add DO BEGIN
     
     command = string(i,format="('this_flag = STRUPCASE( f',I0,' )')")
     errcode = Execute( command )
     
     ind = WHERE( def_flags EQ this_flag ,nind)
     IF (nind EQ 0) THEN BEGIN
        message,'Warning: Flag ' + this_flag + $
                ' is not defined in distance-omnibus. New flag NOT set!',/inf
        RETURN,flag
     ENDIF
     
     ;; Set the flag!
     flag = flag OR !KDAN.(ind)
     
  ENDFOR
  
  IF KEYWORD_SET( verbose ) THEN message,$
     string(flag,format="('New KDA_Note flag is set to ',I0)"),/inf
  
  RETURN,flag
END
